
package com.googlecode.esblabs.util.mule.transformer.jaxb;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.xml.bind.JAXBContext;

import org.mule.api.lifecycle.InitialisationException;
import org.mule.transformer.AbstractMessageAwareTransformer;
import org.mule.util.ClassUtils;

public abstract class AbstractJaxbTransformer extends AbstractMessageAwareTransformer {
    private String jaxbContextFactoryClass;
    protected JAXBContext jaxbContext;

    @Override
    public void initialise() throws InitialisationException
    {
        try
        {
        	if (logger.isDebugEnabled()) logger.debug("Load JAXB Factory class: " + jaxbContextFactoryClass);
        	Class<?> clazz = ClassUtils.loadClass(jaxbContextFactoryClass, this.getClass());
            Method method = clazz.getMethod("getJaxbContext");
            jaxbContext = (JAXBContext)method.invoke(clazz);
        }
        catch (ClassNotFoundException e)
        {
            throw new InitialisationException(e, this);
        }
        catch (NoSuchMethodException e)
        {
            throw new InitialisationException(e, this);
        }
        catch (IllegalAccessException e)
        {
            throw new InitialisationException(e, this);
        }
        catch (InvocationTargetException e)
        {
            throw new InitialisationException(e, this);
        }
    }

    public String getJaxbContextFactoryClass()
    {
        return jaxbContextFactoryClass;
    }

    public void setJaxbContextFactoryClass(String jaxbContextFactoryClass)
    {
        this.jaxbContextFactoryClass = jaxbContextFactoryClass;
    }

    public JAXBContext getJaxbContext()
    {
        return jaxbContext;
    }

    public void setJaxbContext(JAXBContext jaxbContext)
    {
        this.jaxbContext = jaxbContext;
    }

}
